import java.util.LinkedList;

public class BaggageCarousel extends Thread {

	int id;
	int capacity;
	int currentNumber;
	String flightName;
	LinkedList<Passenger> passengerList = new LinkedList<Passenger>();
	Boolean busy = false;
	int waitTime;
	Passenger p;


	public BaggageCarousel(int id, int capacity, int waitTime) {
		this.id = id;
		this.capacity = capacity;
		this.waitTime = waitTime;

	}

	public CustomUnit getMin(LinkedList<CustomUnit> list) {
		CustomUnit cumin;
		cumin = list.getFirst();
		for (int i = 0; i < list.size(); i++) {

			if (list.get(i).queue.size() <= cumin.queue.size()) {
				cumin = list.get(i);
			}
		}

		return cumin;

	} 
	public void update(int n)
	{

		Launch.textArea2.get(this.id - 1).setText("Serves Flight : " + this.flightName + "\n"+ "Finish after : "+ n * this.waitTime / 20000
				+ " Min" +"\n"+this.passengerList.size());
		Launch.textArea2.get(this.id - 1).validate();
		CustomUnit cu = getMin(Launch.CustomsList);
cu.queue.add(p);
		synchronized (cu) {
			if (!cu.isAlive())
				cu.start();
			
		}
	
		//cu.update();
	}

	public void run() {
		while (true) {

			
				synchronized (this) {
					this.notify();

				}

				try {
					synchronized (this) {
						
						for(int i =0 ;i<20;i++)
						{
							if (this.passengerList.size() > 0) 
								{
								p = this.passengerList.remove(0);}
							this.update(this.passengerList.size());

						}
						 
						this.wait(waitTime);

					}
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			
			if (this.passengerList.size() == 0) {
				try {

					synchronized (this) {
						// System.out.println("waaaaaaaaaaaaaait");
						// this.busy = false;
						// this.flightName = "";

						// Launch.textArea2.get(this.id - 1).setText(" Free ");
						Launch.textArea2.get(this.id - 1).validate();
						this.wait(1);

					}

				} catch (InterruptedException e) {
					// TODO Auto-generated catch block

					e.printStackTrace();
				}
			}
		}

	}

}
